package cn.tx.erp.sys.controller;

import cn.tx.erp.sys.service.LogService;
import cn.tx.erp.sys.utils.DataGridView;
import cn.tx.erp.sys.utils.ResultObj;
import cn.tx.erp.sys.vo.LogVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/2/2 17:52
 */
@RestController
@RequestMapping("log")
public class LogController {
    @Autowired
    private LogService logService;

    /**
     * 加载全部登录日志信息
     * @param logVo
     * @return
     */
    @RequestMapping("loadAllLog")
    public DataGridView loadLoginInfo(LogVo logVo) {
        IPage page = new Page(logVo.getPage(), logVo.getLimit());
        //条件构造器
        QueryWrapper queryWrapper = new QueryWrapper();

        //模糊查询
        queryWrapper.like(StringUtils.isNotBlank(logVo.getLoginname()), "loginname", logVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(logVo.getLoginip()), "loginip", logVo.getLoginip());
        //大于等于
        queryWrapper.ge(logVo.getStarttime() != null, "logintime", logVo.getStarttime());
        //小于等于
        queryWrapper.le(logVo.getEndtime() != null, "logintime", logVo.getEndtime());
        queryWrapper.orderByDesc("logintime");
        this.logService.page(page, queryWrapper);
        System.out.println("************************************");
        System.out.println(page.getTotal());
//        System.out.println(page.getRecords());
        return new DataGridView(0, "", page.getTotal(), page.getRecords());
    }

    /**
     * 删除日志信息
     * @param id
     * @return
     */
    @RequestMapping("deleteLog")
    public ResultObj deleteLog(Integer id) {
        try {
            this.logService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERR;
        }
    }

    /**
     * 批量删除
     * @param logVo
     * @return
     */
    @RequestMapping("batchDelete")
    public ResultObj batchDelete(LogVo logVo) {
        try {
            Collection idList = new ArrayList();
            for (Integer id : logVo.getIds()) {
                idList.add(id);
            }
            this.logService.removeByIds(idList);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERR;
        }
    }

}
